using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;

using System.Web;
using System.Web.Caching;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.Mail;
using System.Web.Security;

using System.Net;

using System.Configuration;
using System.IO;

namespace whole
{
    public class RequestState
    {
        const int BufferSize = 1024;
        public StringBuilder RequestData;
        public byte[] BufferRead;
        public WebRequest Request;
        public Stream ResponseStream;
        // Create Decoder for appropriate enconding type.
        public Decoder StreamDecode = Encoding.UTF8.GetDecoder();

        public RequestState()
        {
            BufferRead = new byte[BufferSize];
            RequestData = new StringBuilder(String.Empty);
            Request = null;
            ResponseStream = null;
        }
    }

    public class apiInterface
    {
        private string hostkey = ConfigurationManager.AppSettings["hostkey"].ToString();
        private string apiServer = ConfigurationManager.AppSettings["loginserver"].ToString();
        private string language;

        public apiInterface()
        {
            language = "it";
        }

        public apiInterface(string lang)
        {
            language = lang;
        }

        public string Login(string username, string password)
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(apiServer + "/testApi.aspx?hostkey=" + hostkey + "&command=signon&username=" + username + "&password=" + password + "&language=" + language);

            return xDoc.OuterXml;
        }

        public string Login(string ticket)
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(apiServer + "/login.aspx?hostkey=" + hostkey + "&ticket=" + ticket + "&language=" + language);

            return xDoc.OuterXml;
        }

        public string GetApiCountries(Page page)
        {
            string cacheName = "";

            cacheName = "GetApiCountries";

            // try to retrieve item from cache
            // if it's not there, add it
            XmlDocument doc = (XmlDocument)page.Cache[cacheName];

            if (doc == null)
            {
                doc = new XmlDocument();
                doc.Load(apiServer + "/ajax/util.aspx?command=gimmecountries");
                page.Cache.Add(cacheName, doc, null, DateTime.Now.AddMinutes(60), TimeSpan.Zero, CacheItemPriority.High, null);
            }

            return doc.OuterXml;
        }

        public string GetApiRegions(Page page, string country)
        {
            string cacheName = "";

            cacheName = "GetApiRegions" +"."+ country;

            // try to retrieve item from cache
            // if it's not there, add it
            XmlDocument doc = (XmlDocument)page.Cache[cacheName];

            if (doc == null)
            {
                doc = new XmlDocument();
                doc.Load(apiServer + "/ajax/util.aspx?command=gimmeregions&country=" + country);
                page.Cache.Add(cacheName, doc, null, DateTime.Now.AddMinutes(60), TimeSpan.Zero, CacheItemPriority.High, null);
            }

            return doc.OuterXml;

        }

        public string GetApiCities(string country, string region)
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(apiServer + "/ajax/util.aspx?command=gimmecities&country=" + country + "&region=" + region);

            return xDoc.OuterXml;
        }

        public string GetCity(string citycode)
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(apiServer + "/testApi.aspx?hostkey=" + hostkey + "&command=getcity&citycode=" + citycode);

            return xDoc.OuterXml;
        }

        public string Registration(string username, string password, string country, string region, string city, string sex, string birthdate, string notes, string email)
        {
            XmlDocument xDoc = new XmlDocument();
            string s = apiServer + "/testApi.aspx?hostkey=" + hostkey + "&command=signup&username=" + username + "&password=" + password + "&country=" + country + "&region=" + region + "&city=" + city + "&sex=" + sex + "&birthdate=" + birthdate + "&notes=" + notes + "&email=" + email + "&language=" + language;
            xDoc.Load(apiServer + "/testApi.aspx?hostkey=" + hostkey + "&command=signup&username=" + username + "&password=" + password + "&country=" + country + "&region=" + region + "&city=" + city + "&sex=" + sex + "&birthdate=" + birthdate + "&notes=" + notes + "&email=" + email + "&language=" + language);

            return xDoc.OuterXml;
        }

        public string Update(string userid, string username, string password, string country, string region, string city, string sex, string birthdate, string notes, string email)
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(apiServer + "/testApi.aspx?hostkey=" + hostkey + "&command=updateuser&userid=" + userid + "&username=" + username + "&password=" + password + "&country=" + country + "&region=" + region + "&city=" + city + "&sex=" + sex + "&birthdate=" + birthdate + "&notes=" + notes + "&email=" + email + "&language=" + language);

            return xDoc.OuterXml;
        }

        public string UpdateUserEmail(string userid, string password, string email )
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(apiServer + "/testApi.aspx?hostkey=" + hostkey + "&command=user.email.update&userid=" + userid + "&password=" + password + "&email=" + email + "&language=" + language);

            return xDoc.OuterXml;
        }

        public string Update(Page page, string userid, string username, string password, string country, string region, string city, string sex, string birthdate)
        {
            XmlDocument xDoc = new XmlDocument();

            page.Trace.Write("apicall", apiServer + "/testApi.aspx?hostkey=" + hostkey + "&command=updateuser&userid=" + userid + "&username=" + username + "&password=" + password + "&country=" + country + "&region=" + region + "&city=" + city + "&sex=" + sex + "&birthdate=" + birthdate + "&language=" + language);
            xDoc.Load(apiServer + "/testApi.aspx?hostkey=" + hostkey + "&command=updateuser&userid=" + userid + "&username=" + username + "&password=" + password + "&country=" + country + "&region=" + region + "&city=" + city + "&sex=" + sex + "&birthdate=" + birthdate + "&language=" + language);

            return xDoc.OuterXml;
        }

        public string UserInfo(Page page, string userid)
        {
            XmlDocument xDoc = new XmlDocument();

            page.Trace.Write("freak_call", apiServer + "/testApi.aspx?hostkey=" + hostkey + "&command=userinfo&userid=" + userid + "&language=" + language);

            xDoc.Load(apiServer + "/testApi.aspx?hostkey=" + hostkey + "&command=userinfo&userid=" + userid + "&language=" + language);

            return xDoc.OuterXml;
        }

        public string DeleteUser(string userid)
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(apiServer + "/testApi.aspx?hostkey=" + hostkey + "&command=deleteuser&userid=" + userid + "&language=" + language);

            return xDoc.OuterXml;
        }

        public string ChangePassword(string userid, string username, string oldpassword, string newpassword)
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(apiServer + "/testApi.aspx?hostkey=" + hostkey + "&command=changepassword&userid=" + userid + "&username=" + username + "&oldpassword=" + oldpassword + "&newpassword=" + newpassword + "&language=" + language);

            return xDoc.OuterXml;
        }

        public string Login(string username, string password, string cachedata)
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(apiServer + "/testApi.aspx?hostkey=" + hostkey + "&command=signon&username=" + username + "&password=" + password + "&xmldatacache=" + cachedata + "&language=" + language);

            return xDoc.OuterXml;
        }

        public string GetUsersOnline()
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(apiServer + "/testApi.aspx?hostkey=" + hostkey + "&command=getonlineusers&getmode=crossdomain&language=" + language);

            return xDoc.OuterXml;
        }

        public string KeepAlive(string username, string password, string cachedata)
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(apiServer + "/testApi.aspx?hostkey=" + hostkey + "&command=keepalive&username=" + username + "&password=" + password + "&xmldatacache=" + cachedata + "&language=" + language);

            return xDoc.OuterXml;
        }

        public void KeepAlive(string username)
        {
            System.Net.HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(apiServer + "/testApi.aspx?hostkey=" + hostkey + "&command=keepalive&language=" + language);
            RequestState rs = new RequestState();

            // Add the request into the state so it can be passed around
            rs.Request = wreq;

            // Issue the async request
            IAsyncResult r = (IAsyncResult)wreq.BeginGetResponse(new AsyncCallback(RespCallback), rs);
        }


        public void LogOut(string username)
        {
            HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(apiServer + "/testApi.aspx?hostkey=" + hostkey + "&command=logout&username=" + username + "&language=" + language);
            RequestState rs = new RequestState();

            // Add the request into the state so it can be passed around
            rs.Request = wreq;

            // Issue the async request
            IAsyncResult r = (IAsyncResult)wreq.BeginGetResponse(new AsyncCallback(RespCallback), rs);
        }

        private static void RespCallback(IAsyncResult ar)
        {
            // Get the RequestState object from the async result
            RequestState rs = (RequestState)ar.AsyncState;

            // Get the HttpWebRequest from RequestState
            WebRequest req = rs.Request;

            // Calling EndGetResponse produces the HttpWebResponse object
            // which came from the request issued above
            try
            {
                WebResponse resp = (WebResponse)req.EndGetResponse(ar);
                resp.Close();
            }
            catch (Exception ex)
            {
                ex.ToString();
            }

            // Now that we have the response, it is time to start reading
            // data from the response stream
            //Stream ResponseStream = resp.GetResponseStream();

            //// The read is also done using async so we'll want
            //// to store the stream in RequestState
            //rs.ResponseStream = ResponseStream;

            //// Note that rs.BufferRead is passed in to BeginRead.  This is
            //// where the data will be read into.
            //IAsyncResult iarRead = ResponseStream.BeginRead(rs.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), rs);
        }


    }

    //public class apiInterface
    //{
    //    private string hostkey = ConfigurationManager.AppSettings["hostkey"].ToString();
    //    private string apiServer = ConfigurationManager.AppSettings["loginserver"].ToString();
    //    private string language;

    //    public apiInterface()
    //    {
    //        language = "it";
    //    }

    //    public apiInterface(string lang)
    //    {
    //        language = lang;
    //    }

    //    public string Login(string username, string password)
    //    {
    //        XmlDocument xDoc = new XmlDocument();
    //        xDoc.Load(apiServer + "/testApi.aspx?hostkey=" + hostkey + "&command=signon&username=" + username + "&password=" + password + "&language=" + language);

    //        return xDoc.OuterXml;
    //    }

    //    public string GetApiCountries()
    //    {
    //        XmlDocument xDoc = new XmlDocument();
    //        xDoc.Load(apiServer + "/ajax/util.aspx?command=gimmecountries");

    //        return xDoc.OuterXml;
    //    }

    //    public string GetApiRegions(string country)
    //    {
    //        XmlDocument xDoc = new XmlDocument();
    //        xDoc.Load(apiServer + "/ajax/util.aspx?command=gimmeregions&country=" + country);

    //        return xDoc.OuterXml;
    //    }

    //    public string GetApiCities(string country, string region)
    //    {
    //        XmlDocument xDoc = new XmlDocument();
    //        xDoc.Load(apiServer + "/ajax/util.aspx?command=gimmecities&country=" + country + "&region=" + region);

    //        return xDoc.OuterXml;
    //    }

    //    public string GetCity(string citycode)
    //    {
    //        XmlDocument xDoc = new XmlDocument();
    //        xDoc.Load(apiServer + "/testApi.aspx?hostkey=" + hostkey + "&command=getcity&citycode=" + citycode);

    //        return xDoc.OuterXml;
    //    }

    //    public string Registration(string username, string password, string country, string region, string city, string sex, string birthdate, string notes, string email)
    //    {
    //        XmlDocument xDoc = new XmlDocument();
    //        string s = apiServer + "/testApi.aspx?hostkey=" + hostkey + "&command=signup&username=" + username + "&password=" + password + "&country=" + country + "&region=" + region + "&city=" + city + "&sex=" + sex + "&birthdate=" + birthdate + "&notes=" + notes + "&email=" + email + "&language=" + language;
    //        xDoc.Load(apiServer + "/testApi.aspx?hostkey=" + hostkey + "&command=signup&username=" + username + "&password=" + password + "&country=" + country + "&region=" + region + "&city=" + city + "&sex=" + sex + "&birthdate=" + birthdate + "&notes=" + notes + "&email=" + email + "&language=" + language);
            
    //        return xDoc.OuterXml;
    //    }

    //    public string Update(string userid, string username, string password, string country, string region, string city, string sex, string birthdate, string notes, string email)
    //    {
    //        XmlDocument xDoc = new XmlDocument();
    //        xDoc.Load(apiServer + "/testApi.aspx?hostkey=" + hostkey + "&command=updateuser&userid=" + userid + "&username=" + username + "&password=" + password + "&country=" + country + "&region=" + region + "&city=" + city + "&sex=" + sex + "&birthdate=" + birthdate + "&notes=" + notes + "&email=" + email + "&language=" + language);

    //        return xDoc.OuterXml;
    //    }

    //    public string Update(string userid, string username, string password, string country, string region, string city, string sex, string birthdate )
    //    {
    //        XmlDocument xDoc = new XmlDocument();
    //        xDoc.Load(apiServer + "/testApi.aspx?hostkey=" + hostkey + "&command=updateuser&userid=" + userid + "&username=" + username + "&password=" + password + "&country=" + country + "&region=" + region + "&city=" + city + "&sex=" + sex + "&birthdate=" + birthdate + "&language=" + language);

    //        return xDoc.OuterXml;
    //    }

    //    public string Update(string userid, string username, string country, string region, string city, string sex, string birthdate)
    //    {
    //        XmlDocument xDoc = new XmlDocument();
    //        xDoc.Load(apiServer + "/testApi.aspx?hostkey=" + hostkey + "&command=updateuser&userid=" + userid + "&username=" + username + "&country=" + country + "&region=" + region + "&city=" + city + "&sex=" + sex + "&birthdate=" + birthdate + "&language=" + language);

    //        return xDoc.OuterXml;
    //    }

    //    public string DeleteUser(string userid)
    //    {
    //        XmlDocument xDoc = new XmlDocument();
    //        xDoc.Load(apiServer + "/testApi.aspx?hostkey=" + hostkey + "&command=deleteuser&userid=" + userid + "&language=" + language);

    //        return xDoc.OuterXml;
    //    }

    //    public string ChangePassword(string userid, string username, string oldpassword, string newpassword)
    //    {
    //        XmlDocument xDoc = new XmlDocument();
    //        xDoc.Load(apiServer + "/testApi.aspx?hostkey=" + hostkey + "&command=changepassword&userid=" + userid + "&username=" + username + "&oldpassword=" + oldpassword + "&newpassword=" + newpassword + "&language=" + language);

    //        return xDoc.OuterXml;
    //    }
        
    //    public string Login(string username, string password, string cachedata)
    //    {
    //        XmlDocument xDoc = new XmlDocument();
    //        xDoc.Load(apiServer + "/testApi.aspx?hostkey=" + hostkey + "&command=signon&username=" + username + "&password=" + password + "&xmldatacache=" + cachedata + "&language=" + language);

    //        return xDoc.OuterXml;
    //    }

    //    public string GetUsersOnline()
    //    {
    //        XmlDocument xDoc = new XmlDocument();
    //        xDoc.Load(apiServer + "/testApi.aspx?hostkey=" + hostkey + "&command=getonlineusers&language=" + language);

    //        return xDoc.OuterXml;
    //    }

    //    public void KeepAlive(string username)
    //    {
    //        HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(apiServer + "/testApi.aspx?hostkey=" + hostkey
    //            + "&command=keepalive&language=" + language
    //            + "&username=" + username);
    //        RequestState rs = new RequestState();
            
    //        // Add the request into the state so it can be passed around
    //        rs.Request = wreq;

    //        // Issue the async request
    //        IAsyncResult r = (IAsyncResult)wreq.BeginGetResponse(new AsyncCallback(RespCallback), rs);
    //    }


    //    public void LogOut(string username)
    //    {
    //        HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(apiServer + "/testApi.aspx?hostkey=" + hostkey + "&command=logout&username=" + username + "&language=" + language);
    //        RequestState rs = new RequestState();

    //        // Add the request into the state so it can be passed around
    //        rs.Request = wreq;

    //        // Issue the async request
    //        IAsyncResult r = (IAsyncResult)wreq.BeginGetResponse(new AsyncCallback(RespCallback), rs);
    //    }

    //    private static void RespCallback(IAsyncResult ar)
    //    {
    //        // Get the RequestState object from the async result
    //        RequestState rs = (RequestState)ar.AsyncState;

    //        // Get the HttpWebRequest from RequestState
    //        WebRequest req = rs.Request;
            
    //        // Calling EndGetResponse produces the HttpWebResponse object
    //        // which came from the request issued above
    //        try
    //        {
    //            WebResponse resp = (WebResponse)req.EndGetResponse(ar);
    //            resp.Close();
    //        }
    //        catch (Exception ex)
    //        {

    //        }
            
    //        // Now that we have the response, it is time to start reading
    //        // data from the response stream
    //        //Stream ResponseStream = resp.GetResponseStream();

    //        //// The read is also done using async so we'll want
    //        //// to store the stream in RequestState
    //        //rs.ResponseStream = ResponseStream;

    //        //// Note that rs.BufferRead is passed in to BeginRead.  This is
    //        //// where the data will be read into.
    //        //IAsyncResult iarRead = ResponseStream.BeginRead(rs.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), rs);
    //    }


    //}
}
